package com.rxkj.service.impl;

import com.rxkj.entity.sys.NavMenu;
import com.rxkj.entity.sys.NavMenuVo;
import com.rxkj.mapper.NavMenuMapper;
import com.rxkj.service.NavMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class NavMenuServiceImpl implements NavMenuService {

    @Autowired
    NavMenuMapper navMenuMapper;

    @Override
    public List<NavMenu> getNavMenus(Integer id, String userName) {
        return navMenuMapper.getNavMenus(id, userName);
    }

    @Override
    public List<NavMenu> getNavMenu(Integer id, String userName) {
        List<NavMenu> menu = navMenuMapper.getNavMenu(id, userName);
        getTree(menu);
        return menu;
    }

    @Override
    public List<NavMenu> getTreeMenus() {
        List<NavMenu> menu = navMenuMapper.getTreeMenus();
        getTree(menu);
        return menu;
    }

    @Override
    public List<NavMenu> getRoleMenus(Integer id, String roleName) {
        List<NavMenu> menus = navMenuMapper.getRoleMenus(id, roleName);
        getTree(menus);
        return menus;
    }

    @Override
    public List<NavMenu> getTree(List<NavMenu> menu) {
        List<NavMenu> temp = new ArrayList<>();
        menu.forEach(navMenu -> {
            List<NavMenu> childList = new ArrayList<>();
            menu.stream().filter(childMenu -> navMenu.getId().equals(childMenu.getPid())).forEach(childMenu -> {
                childList.add(childMenu);
                temp.add(childMenu);
            });
            navMenu.setChildMenu(childList);
        });
        if (temp.size() > 0) {
            menu.removeAll(temp);
        }
        return menu;
    }

    @Transactional
    @Override
    public void addRoleMenus(Integer id, Integer[] authority) {
        navMenuMapper.delete(id);
        navMenuMapper.addRoleMenus(id, authority);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer AddMenus(NavMenu navMenu) {
        int info = navMenuMapper.getMenuUrlMenus(navMenu.getMenuUrl());
        if (info > 0) {
            return 0;
        }
        navMenuMapper.AddMenus(navMenu);
        return navMenuMapper.AddRoleMenus(navMenuMapper.getRoleId(), navMenu.getId());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void DeleteMenu(Integer id) {
        navMenuMapper.DeleteMenu(id);
        navMenuMapper.DeleteRoleMenu(id);
        DeleteByPid(id);
    }

    private void DeleteByPid(Integer pid) {
        List<Integer> childrenIds = navMenuMapper.getChildrenIdsByPid(pid);
        if (childrenIds != null && childrenIds.size() > 0) {
            for (Integer childrenId : childrenIds) {
                navMenuMapper.DeleteMenu(childrenId);
                navMenuMapper.DeleteRoleMenu(childrenId);
                DeleteByPid(childrenId);
            }
        }

    }

    @Override
    public int EditMenu(NavMenu navMenu) {
        return navMenuMapper.EditMenu(navMenu);
    }

    @Override
    public List<NavMenuVo> getAllMenus() {
        List<NavMenuVo> menu = navMenuMapper.getAllMenus();
        List<NavMenuVo> temp = new ArrayList<>();
        menu.forEach(navMenu -> {
            List<NavMenuVo> childList = new ArrayList<>();
            menu.stream().filter(childMenu -> navMenu.getId().equals(childMenu.getPid())).forEach(childMenu -> {
                childList.add(childMenu);
                temp.add(childMenu);
            });
            navMenu.setChildren(childList);
        });
        if (temp.size() > 0) {
            menu.removeAll(temp);
        }
        return menu;
    }

    @Override
    public NavMenu getMenuId(Integer id) {
        return navMenuMapper.getMenuId(id);
    }
}
